<?php

namespace app\api\controller;

use app\admin\model\department\Lists;
use app\admin\model\Dynamic;
use app\admin\model\product\Order;
use app\admin\model\Slide;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 获取配置信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getConfigs()
    {
        $name = $this->request->get('name');
        if (!$name) {
            $this->error('参数缺失！');
        }
        $config = Config::field('type,value,content')->where('name', $name)->find();
        if (!$config) {
            $this->error('该内容不存在！');
        }

        $this->success('配置信息为', ['type' => $config->type, 'value' => $config->value, 'content' => $config->content]);
    }

    /**
     * 首页轮播图
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSlides()
    {
        $type = input('type', 'index');
        $list = Slide::field('id,title,url,image')->where('status', 'normal')->where('type', $type)->order('id asc')->select();
        foreach ($list as &$val) {
            $val['image'] = make_full_url($val['image']);
        }
        $this->success('轮播图为', $list);
    }

    /**
     * 公告
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDynamics()
    {
        $list = Dynamic::field('id,content')->where('type', 1)->order('id desc')->select();
        $this->success('轮播图为', $list);
    }

    /**
     * 物资分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cates()
    {
        $list = \app\admin\model\product\Category::field('id,title')->where('status', 'normal')->order('sort asc,id desc')->select();
        $this->success('物资分类', $list);
    }
}
